const DB = require('../../../utils/db')
// 引入jwt token工具
const JwtUtil = require('../../../utils/jwt')
/**
 * GET
 * 获取单个动态详情
 */
module.exports = async (ctx) => {
  // 获取token
  
  let token = ctx.request.header.authorization

  let jwt = new JwtUtil(token)
  // 用token获取到userid
  let userid = jwt.verifyToken()
  
  let id = ctx.request.query.id
  const res = await DB.query(
    `select userid, createTime, label, textContent, imgUrl, title, heartId from  dynamic where id='${id}'`
  )

  res.forEach(function (item, index) {
    let heartId = item.heartId;
    
    // true表示点赞， false表示没有点赞
    let isHeart = false
    if (heartId !== null) {
     heartId.split(',').forEach(function (item, index) {
       if (item === userid) {
         isHeart = true
       }
     })
    }
    item.isHeart = isHeart;
  })

  const user = await DB.query(
    `select userid, username, avatar from user where userid='${res[0].userid}'`
  )
  ctx.body = {
    code: 0,
    data: { ...res[0], ...user[0] },
  }
}
